package edu.byu.intex2.data.dao;

import java.sql.*;
import java.util.List;
import java.util.LinkedList;

/**
 * A singleton object that CRUD's ConceptualCar objects.
 *
 * @author Conan C. Albrecht <conan@warp.byu.edu>
 * @version 2008-03-03
 */
public class ConceptualCarDAO{
    
  /////////////////////////////////////////////
  ///   Singleton code
  
  private static ConceptualCarDAO instance = null;
  
  /** Creates a new instance of ConceptualCarDAO */
  private ConceptualCarDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized ConceptualCarDAO getInstance() {
    if (instance == null) {
      instance = new ConceptualCarDAO();
    }
    return instance;
  }//getInstance
  
  
  ////////////////////////////////////////////
  ///   CREATE methods
  
  /** Creates a new conceptualCar in the database */
  public ConceptualCar create() throws DataException {    
	//create a new blank conceptualCar object without any parameters in the constructor
	//set the alreadyInDB variable to false
	//put to cache and return the new object	
  }
  
  
  
  ////////////////////////////////////////////
  ///   READ methods
  
  /** Reads an existing conceptualCar from the database */
  public ConceptualCar read(String id) throws DataException {
    //check to see if the object is in cache, if it is just return it
	//establish a connection from the pool, then pass that connection along with the string id to the next method
	//catch any exceptions you may have
	//return the conceptualCar
	//RELEASE THE CONNECTION!!		
  }
  
  /** Internal method to read an existing conceptualCar from the database */
  synchronized ConceptualCar read(String id, Connection conn) throws Exception {
    //check the cache just like above just to make sure
	//use a prepared statement to prevent against sql injection
	//run the query and get the result set. have the next method process the result set
	//catch any exceptions you may have
	//return the conceptualCar
	
  }//read
  
  
  /** Internal method to create a conceptualCar object from a record */
  synchronized ConceptualCar readRecord(ResultSet rs, Connection conn) throws Exception {
    //again, check the cache, but check it with the id from the record set that was passed
	//create a new conceptualCar object. don't forget to put it to cache, set it's alreadyInDB param to true and dirty to false
	//return the conceptualCar
	
  }//readRecord
  

  
  /////////////////////////////////////////////
  ///   UPDATE methods
  
  /** Saves an existing conceptualCar in the database */
  public void save(ConceptualCar conceptualCar) throws DataException {
    //get a connection from the pool, save all of the sales first using the other conceptualCar method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!
	
  }
  
  /** Internal method to update a conceptualCar in the database */
  void save(ConceptualCar conceptualCar, Connection conn) throws Exception {
    //check cache first, if returns nothing, put the conceptualCar object to cache
	//decide whether to update or insert into the database
	
    }  
  
  /** Saves an existing conceptualCar to the database */
  private void update(ConceptualCar conceptualCar, Connection conn) throws Exception {
    
	//use a prepared statement to prevent sql injection
	//run the query and set dirty to fase
	//make sure to close the prepared statement
	
  }
  
  /** Inserts a new conceptualCar into the database */
  private void insert(ConceptualCar conceptualCar, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement
	
  }  
  
  
  
  
  
  /////////////////////////////////////////////////
  ///   DELETE methods
  
  /** We do not support deleting of business objects in this application */
  public void delete(ConceptualCar conceptualCar) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }



  ////////////////////////////////////////////////
  ///   SEARCH methods

  /** Retrieves all conceptualcars from the database */
  public List<ConceptualCar> getAll() throws DataException {
    //establish a connection, use a prepared statement, plug it into the search method return a list of Concept Cars
    }
  }

  /** Internal method to search by certain fields */
  private List<ConceptualCar> search(PreparedStatement stmt, Connection conn) throws Exception {
    List<ConceptualCar> conceptualcar = new LinkedList<ConceptualCar>();
    try {
      ResultSet rs = stmt.executeQuery();
      while (rs.next()) {
        conceptualcar.add(readRecord(rs, conn));
      }
    }finally{
      stmt.close();
    }
    return conceptualcar;
  }

  public List<ConceptualCar> getByMakeModel(String make, String model) throws Data Exception{
	//establish connection and prepared statement run it through the search method, return a list of cars matching the make and model(different years abvailable)
  }
  
  public ConceptualCar getByMakeModelYear(String make, String model, String year) throws Data Exception{
	//establish connection and prepared statement run it through the search method, return a Conceptual Cars matching the make and model and year
  }


}//class
